package com.atguigu.tingshu.order.api;

import com.atguigu.tingshu.common.login.GuiguLogin;
import com.atguigu.tingshu.common.result.Result;
import com.atguigu.tingshu.common.util.AuthContextHolder;
import com.atguigu.tingshu.model.order.OrderInfo;
import com.atguigu.tingshu.order.mapper.OrderInfoMapper;
import com.atguigu.tingshu.order.service.OrderInfoService;
import com.atguigu.tingshu.vo.order.OrderInfoVo;
import com.atguigu.tingshu.vo.order.TradeVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@Tag(name = "订单管理")
@RestController
@RequestMapping("api/order")
@SuppressWarnings({"all"})
public class OrderInfoApiController {

    @Autowired
    private OrderInfoService orderInfoService;
    @Qualifier("orderInfoMapper")
    @Autowired
    private OrderInfoMapper orderInfoMapper;

    /**
     * 订单结算（渲染订单结算页面三种不同的商品列表，包括：会员、专辑、声音）
     *
     * @param tradeVo 项目类别，项目id，声音数量
     * @return
     */
    @Operation(summary = "订单结算（渲染订单结算页面三种不同的商品列表，包括：会员、专辑、声音）")
    @PostMapping("/orderInfo/trade")
    @GuiguLogin
    public Result<OrderInfoVo> tradeData(@RequestBody TradeVo tradeVo) {
        Long userId = AuthContextHolder.getUserId();
        OrderInfoVo orderInfoVo = orderInfoService.tradeData(userId, tradeVo);
        return Result.ok(orderInfoVo);
    }

    /**
     * 订单提交(包含两种付款方式 1.余额支付 2.微信支付)
     *
     * @param orderInfoVo
     * @return
     */
    @GuiguLogin
    @PostMapping("/orderInfo/submitOrder")
    @Operation(summary = "订单提交(包含两种付款方式 1.余额支付 2.微信支付)")
    public Result<Map<String, String>> submitOrder(@Validated @RequestBody OrderInfoVo orderInfoVo) {
        Long userId = AuthContextHolder.getUserId();
        Map<String, String> map = orderInfoService.submitOrder(userId, orderInfoVo);
        return Result.ok(map);
    }

    /**
     * 根据订单号获取订单详细信息(包括订单明细以及优惠列表)
     *
     * @param orderNo
     * @return
     */
    @Operation(summary = "根据订单号获取订单详细信息(包括订单明细以及优惠列表)")
    @GetMapping("/orderInfo/getOrderInfo/{orderNo}")
    public Result<OrderInfo> getOrderInfo(@PathVariable String orderNo) {
        OrderInfo orderInfo = orderInfoService.getOrderInfo(orderNo);
        return Result.ok(orderInfo);
    }

    /**
     * 分页获取当前用户的订单列表
     *
     * @param page
     * @param limit
     * @return
     */
    @Operation(summary = "分页获取当前用户的订单列表")
    @GuiguLogin
    @GetMapping("/orderInfo/findUserPage/{page}/{limit}")
    public Result<Page<OrderInfo>> getUserOrderByPage(@PathVariable Integer page, @PathVariable Integer limit) {
        Long userId = AuthContextHolder.getUserId();

        //封装分页对象
        Page<OrderInfo> orderInfoPage = new Page<>(page, limit);
        orderInfoPage = orderInfoService.getUserOrderByPage(orderInfoPage, userId);
        return Result.ok(orderInfoPage);
    }

}

